package mtm.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import mtm.bp.MultTableGenerator;
import mtm.dto.MultSample;
import mtm.dto.Statistics;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MultTableController {

    private static final String SESSION_KEY_SAMPLES = "samples";
    private static final String SESSION_KEY_STATS = "stats";
    private static final String SESSION_KEY_CURRENT_SAMPLE_IDX = "currentSampleIdx";

    private static final String CURRENT_SAMPLE = "currentSample";
    private static final String CURRENT_STATS = "currentStats";

    private MultTableGenerator multTableGenerator;

    public void setMultTableGenerator(MultTableGenerator multTableGenerator) {
        this.multTableGenerator = multTableGenerator;
    }

    @RequestMapping(
            value = "/hello",
            method = RequestMethod.GET)
    public String sayHello() {
        return "WEB-INF/pages/test.jsp";
    }

    @RequestMapping(
            value = "/start",
            method = RequestMethod.GET)
    public ModelAndView startTest(HttpSession session) {
        Statistics stats = new Statistics();
        session.setAttribute(SESSION_KEY_STATS, stats);
        List<MultSample> testSamples = multTableGenerator.generateTestSamples();
        session.setAttribute(SESSION_KEY_SAMPLES, testSamples);
        session.setAttribute(SESSION_KEY_CURRENT_SAMPLE_IDX, 0);
        ModelAndView res = new ModelAndView();
        res.setViewName("WEB-INF/pages/test.jsp");
        res.addObject(CURRENT_SAMPLE, testSamples.get(0));
        return res;
    }

    @RequestMapping(
            value = "/test",
            method = RequestMethod.GET)
    public ModelAndView startTest(@RequestParam("answer") String answerString, HttpSession session) {
        int answer = Integer.valueOf(answerString);
        Statistics stats = (Statistics) session.getAttribute(SESSION_KEY_STATS);
        List<MultSample> testSamples = (List<MultSample>) session.getAttribute(SESSION_KEY_SAMPLES);
        Integer currentSampleIdx = (Integer) session.getAttribute(SESSION_KEY_CURRENT_SAMPLE_IDX);

        MultSample ms = testSamples.get(currentSampleIdx);
        if (answer == ms.getResult()) {
            stats.increaseCorrect();
        } else {
            stats.increaseIncorrect();
        }
        if (currentSampleIdx < testSamples.size() - 1) {
            currentSampleIdx = currentSampleIdx + 1;
            session.setAttribute(SESSION_KEY_CURRENT_SAMPLE_IDX, currentSampleIdx);
            ModelAndView res = new ModelAndView();
            res.setViewName("WEB-INF/pages/test.jsp");
            res.addObject(CURRENT_SAMPLE, testSamples.get(currentSampleIdx));
            return res;
        }
        ModelAndView res = new ModelAndView();
        res.setViewName("WEB-INF/pages/result.jsp");
        res.addObject(CURRENT_STATS, stats);
        return res;
    }
}
